<?php
namespace app\index\model;
use think\Db;
use think\Model;
class Personals extends Model
{
    //个人中心查询
    public static function centerSel($uid){
         $user_center=Db::name('user_info')->where(['uid'=>$uid])->find();
         return $user_center;
    }
    //个人详情查询
    public static function Mydetails($uid){
         $userOne=Db::name('user_info')
                   ->where(['uid'=>$uid]) 
                   ->find();//
         return $userOne;
    }
     //标签查询
    public static function labelSel($uid,$labeid){
              $rd=Db('user_info')
              ->where(['uid'=>$uid])
              ->field('key_word,exerciseId,fooId,footprintId')
              ->find();
              if($labeid==1){
                $user_nature=explode(',', $rd['key_word']);//个性标签
                $key_word=$rd['key_word'];
              }
              if($labeid==2){
                $user_nature=explode(',', $rd['exerciseId']);//我喜欢的运动
                $key_word=$rd['exerciseId'];
              }
             
              if($labeid==3){
                $user_nature=explode(',', $rd['fooId']);//我喜欢的食物
                $key_word=$rd['fooId'];
              }
              
              if($labeid==4){
                $user_nature=explode(',', $rd['footprintId']);//我的足迹
                $key_word=$rd['footprintId'];
              }
              //查询系统定义的
              $labList=Db::name('keyword')->where(['key_type'=>$labeid,'uid'=>'0'])->field('id,keyName')->limit(10)->select();
                    foreach($labList as $k=>$v){
                        if(in_array($v['id'],$user_nature)){
                           $labList[$k]['state']=1;//选中状态
                        }else{
                          $labList[$k]['state']=0;//未选择
                        } 
                    }
                       $keyword=Db::name('keyword')->where('id','in',$key_word)->where(['uid'=>$uid])->field('id,keyName')->limit(10)->select();
                       if(!empty($keyword)){
                        foreach($keyword as $key=>$val){
                             $keyword[$key]['state']=1;//选中状态
                        }
                        //数组追加
                        $labList=  array_merge_recursive($labList,$keyword);
                       }
                     
                   
                
         return $labList;
    }
    /*标签修改*/
     public static function labelSet($uid,$labeid,$keyId){
              if($labeid==1){
                $data=['key_word'=>$keyId];
               Db::name('keyword')->where('id','not in',$keyId)->where(['uid'=>$uid,'key_type'=>$labeid])->delete();
              }
              if($labeid==2){
                 $data=['exerciseId'=>$keyId];//我喜欢的运动
                  Db::name('keyword')->where('id','not in',$keyId)->where(['uid'=>$uid,'key_type'=>$labeid])->delete();
              }
              if($labeid==3){
                 $data=['fooId'=>$keyId];//我喜欢的食物
                  Db::name('keyword')->where('id','not in',$keyId)->where(['uid'=>$uid,'key_type'=>$labeid])->delete();
              }
              if($labeid==4){
                 $data=['footprintId'=>$keyId];//我的足迹
                  Db::name('keyword')->where('id','not in',$keyId)->where(['uid'=>$uid,'key_type'=>$labeid])->delete();
              }
              $rs=Db::name('user_info')->where(['uid'=>$uid])->update($data);
                   
         return $rs;
    }
    //注册数据库操作
    public static function Myadd($openId,$data){
         $rd=Db::name('user_info')->where(['openId'=>$openId])->update($data);
         return $rd;
    }
    //编辑个人资料
    public static function Myset($uid,$data){
         $rd=Db::name('user_info')->where(['uid'=>$uid])->update($data);
         return $rd;
    }
    //忘记密码
    public static function forgetSet($uid,$data){
        $forget=Db::name('user_info')->where(['uid'=>$uid])->update($data);
         return $forget; 
    } 
   
}